package c.f.b.a.a.a.b;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.huawei.appmarket.component.buoycircle.impl.delegete.BuoyBridgeActivity;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.game.a0;
import com.huawei.hms.game.e0;
import com.huawei.hms.game.f0;
import com.huawei.hms.game.h;
import com.huawei.hms.game.h0;
import com.huawei.hms.game.i;
import com.huawei.hms.game.j;
import com.huawei.hms.game.k;
import com.huawei.hms.game.l;
import com.huawei.hms.game.m;

public class g extends LinearLayout {
    public static final /* synthetic */ int z = 0;
    public WindowManager a;
    public LayoutParams b;
    public float c;
    public float d;
    public float e;
    public float f;
    public float g;
    public float h;
    public int i;
    public int j;
    public View k = null;
    public ImageView l = null;
    public ImageView m = null;
    public Context n = null;
    public boolean o = false;
    public Handler p = null;
    public Runnable q = null;
    public boolean r = true;
    public boolean s = false;
    public com.huawei.hms.game.a t;
    public e u;
    public int v;
    public int w;
    public int x;
    public com.huawei.hms.game.j.c y = new a();

    public class c implements Runnable {
        public void run() {
            if (g.this.l.getVisibility() == 8) {
                g.this.g(true);
            }
        }
    }

    public class d implements Runnable {
        public void run() {
            g gVar = g.this;
            int b = m.l().b();
            int c = m.l().c();
            int i = g.z;
            gVar.c(b, c);
        }
    }

    public static class e implements AnimationListener {
        public e(a aVar) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationStart(Animation animation) {
        }
    }

    public interface f {
    }

    public class a implements com.huawei.hms.game.j.c {
        public void a() {
            g gVar = g.this;
            com.huawei.hms.game.a aVar = gVar.t;
            if (aVar != null && !e0.b(gVar.n, aVar.c())) {
                com.huawei.hms.game.d a = com.huawei.hms.game.d.a();
                g gVar2 = g.this;
                a.c(gVar2.n, gVar2.t);
                l a2 = l.a();
                gVar2 = g.this;
                if (a2.a(gVar2.n, gVar2.t)) {
                    a2 = l.a();
                    gVar2 = g.this;
                    a2.c(gVar2.n, gVar2.t);
                    g.this.a();
                    g.this.c(m.l().b(), m.l().c());
                }
                j.b().a();
            }
        }
    }

    public class b implements f {
    }

    public g(Context context, com.huawei.hms.game.a aVar) {
        super(context);
        String str = "FloatWindowSmallView";
        h.a(str, "start create FloatWindowSmallView");
        this.a = (WindowManager) context.getSystemService("window");
        LayoutInflater.from(context).inflate(f0.d("c_buoycircle_window_small"), this);
        this.k = findViewById(f0.c("small_window_layout"));
        setCenterXY(context);
        this.n = context;
        this.l = (ImageView) findViewById(f0.c("half_hide_small_icon"));
        this.m = (ImageView) findViewById(f0.c("small_icon"));
        this.l.setImageAlpha(153);
        this.u = new e(context);
        e eVar = this.u;
        if (!(eVar.getLayoutParams() instanceof FrameLayout.LayoutParams)) {
            Context context2 = eVar.getContext();
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(h0.a(context2, (float) 6), h0.a(context2, (float) 6), 53);
            layoutParams.rightMargin = h0.a(context2, (float) null);
            layoutParams.leftMargin = h0.a(context2, (float) null);
            layoutParams.topMargin = h0.a(context2, (float) null);
            layoutParams.bottomMargin = h0.a(context2, (float) null);
            eVar.setLayoutParams(layoutParams);
        }
        eVar.setBackgroundResource(f0.b("c_buoycircle_red_dot"));
        eVar.setGravity(17);
        eVar.setBadgeCount("");
        this.u.setTargetView(this.k);
        this.w = context.getResources().getConfiguration().orientation;
        this.v = h0.g(context);
        this.t = aVar;
        h.a(str, "finish create FloatWindowSmallView");
    }

    private void setCenterXY(Context context) {
        this.i = h0.e(context);
        this.j = h0.h(context);
    }

    private void setParams(LayoutParams layoutParams) {
        this.b = layoutParams;
        m();
    }

    public void a() {
        StringBuilder g = c.b.a.a.a.g("refreshVisible:");
        g.append(m.l().e());
        h.c("FloatWindowSmallView", g.toString());
        if (m.l().e() || l.a().a(this.n, this.t)) {
            if (getVisibility() != 4) {
                setVisibility(4);
            }
        } else if (getVisibility() != 0) {
            setVisibility(0);
            c(m.l().b(), m.l().c());
            j();
            e(false);
            m.l().b(this.n);
        }
    }

    public final void b(float f, float f2, float f3, float f4) {
        e eVar;
        int i;
        f3 = f - f3;
        int i2 = this.v;
        float f5 = (f2 - ((float) i2)) - f4;
        f4 = ((float) this.j) - f;
        float f6 = ((float) (this.i - i2)) - f2;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("left:");
        stringBuilder.append(f);
        stringBuilder.append(",right:");
        stringBuilder.append(f4);
        stringBuilder.append(",up:");
        stringBuilder.append(f2);
        stringBuilder.append(",down:");
        stringBuilder.append(f6);
        h.a("FloatWindowSmallView", stringBuilder.toString());
        r3 = new float[4];
        int i3 = 0;
        r3[0] = f;
        r3[1] = f2;
        r3[2] = f4;
        r3[3] = f6;
        f6 = r3[0];
        this.x = 0;
        while (i3 < 4) {
            if (r3[i3] < f6) {
                f6 = r3[i3];
                this.x = i3;
            }
            i3++;
        }
        i2 = this.x;
        if (i2 == 1) {
            f5 = 0.0f;
        } else if (i2 == 2) {
            f3 = (float) this.j;
        } else if (i2 != 3) {
            f3 = 0.0f;
        } else {
            f5 = (float) this.i;
        }
        LayoutParams layoutParams = this.b;
        layoutParams.x = (int) f3;
        layoutParams.y = (int) f5;
        if (i2 != 0) {
            if (i2 == 1) {
                eVar = this.u;
                i = 85;
            } else if (i2 == 2) {
                eVar = this.u;
                i = 51;
            } else if (i2 != 3) {
                return;
            }
            eVar.setBadgeGravity(i);
        }
        eVar = this.u;
        i = 53;
        eVar.setBadgeGravity(i);
    }

    public final void c(int i, int i2) {
        LayoutParams layoutParams = this.b;
        if (layoutParams != null) {
            layoutParams.x = i;
            layoutParams.y = i2;
            if (com.huawei.hms.game.f.a().c(this.n) && l()) {
                h();
            }
            n();
        }
    }

    public void d(LayoutParams layoutParams) {
        if (layoutParams != null) {
            setParams(layoutParams);
            b((float) layoutParams.x, (float) layoutParams.y, 0.0f, 0.0f);
            layoutParams.x = m.l().b();
            layoutParams.y = m.l().c();
            if (com.huawei.hms.game.f.a().c(this.n) && l()) {
                h();
            }
            this.l.setVisibility(0);
            this.m.setVisibility(8);
            this.r = true;
            k();
            e(false);
            m.l().b(this.n);
        }
    }

    public void e(boolean z) {
        TextView textView;
        int i;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("showRedPoint:");
        stringBuilder.append(z);
        h.a("FloatWindowSmallView", stringBuilder.toString());
        if (z) {
            textView = this.u;
            i = 0;
        } else {
            textView = this.u;
            i = 8;
        }
        textView.setVisibility(i);
    }

    public void f() {
        Handler handler = this.p;
        if (handler != null) {
            Runnable runnable = this.q;
            if (runnable != null) {
                handler.removeCallbacks(runnable);
            }
        }
    }

    public final boolean g(boolean z) {
        boolean z2 = this.r;
        if (z) {
            ImageView imageView = this.m;
            b bVar = new b();
            if (imageView != null) {
                AnimationSet animationSet = new AnimationSet(true);
                RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 359.0f, 1, 0.5f, 1, 0.5f);
                rotateAnimation.setDuration(300);
                rotateAnimation.setAnimationListener(new h(bVar));
                animationSet.addAnimation(rotateAnimation);
                imageView.startAnimation(animationSet);
            }
        } else {
            this.l.setVisibility(8);
            this.m.setVisibility(0);
            f fVar = new f();
            fVar.a(6, 6, 0, 0, 0, 0);
            this.u.setBadgeLayoutParams(fVar);
            this.r = false;
            this.k.setX(0.0f);
            this.k.setY(0.0f);
        }
        if (z2 == this.r) {
            return true;
        }
        return false;
    }

    public int getTopBarHeight() {
        return this.v;
    }

    public void h() {
        h.c("FloatWindowSmallView", "set small view cutout position");
        com.huawei.hms.game.g b = com.huawei.hms.game.f.a().b(this.n);
        if (b != null && b.c() != null) {
            int a = (int) h0.a(this.n, 48);
            Rect c;
            int i;
            int i2;
            int i3;
            int i4;
            LayoutParams layoutParams;
            int i5;
            if (b.b() == 2) {
                c = b.c();
                i = c.bottom;
                i2 = c.top;
                i3 = ((i - i2) / 2) + i2;
                i4 = a / 2;
                layoutParams = this.b;
                i5 = layoutParams.y;
                i4 += i5;
                int i6 = this.v;
                i4 += i6;
                i5 += i6;
                if ((i5 + a) + i6 >= i2 && i4 <= i3) {
                    layoutParams.y = (i2 - a) - i6;
                } else if (i4 >= i3 && i5 <= i) {
                    layoutParams.y = i - i6;
                }
            } else if (b.b() == 1) {
                c = b.c();
                i = c.right;
                i2 = c.left;
                i3 = ((i - i2) / 2) + i2;
                i4 = a / 2;
                layoutParams = this.b;
                i5 = layoutParams.x;
                i4 += i5;
                if (i5 + a >= i2 && i4 <= i3) {
                    layoutParams.x = i2 - a;
                } else if (i4 >= i3 && i5 <= i) {
                    layoutParams.x = i;
                }
            }
        }
    }

    public final void i() {
        if (this.p == null) {
            this.p = new Handler();
        }
        if (this.q == null) {
            this.q = new c();
        }
        this.p.postDelayed(this.q, 2000);
    }

    public final void j() {
        this.l.setVisibility(0);
        this.m.setVisibility(8);
        this.r = true;
        k();
    }

    public final void k() {
        View view;
        int i;
        int i2;
        int i3;
        int i4;
        int i5;
        int i6;
        float a = h0.a(this.n, 24);
        f fVar = new f();
        StringBuilder g = c.b.a.a.a.g("hideViewByRule, currentPosition:");
        g.append(this.x);
        g.append(",hideWidth:");
        g.append(a);
        h.a("FloatWindowSmallView", g.toString());
        int i7 = this.x;
        if (i7 != 0) {
            if (i7 == 1) {
                view = this.k;
                a *= -1.0f;
            } else if (i7 == 2) {
                view = this.k;
            } else if (i7 == 3) {
                view = this.k;
            } else {
                return;
            }
            view.setY(a);
            i = 6;
            i2 = 6;
            i3 = 6;
            i4 = 6;
            i5 = 30;
            i6 = 30;
            fVar.a(i, i2, i3, i4, i5, i6);
            this.u.setBadgeLayoutParams(fVar);
        }
        view = this.k;
        a *= -1.0f;
        view.setX(a);
        i = 6;
        i2 = 6;
        i3 = 30;
        i4 = 30;
        i5 = 6;
        i6 = 6;
        fVar.a(i, i2, i3, i4, i5, i6);
        this.u.setBadgeLayoutParams(fVar);
    }

    public final boolean l() {
        if (this.n != null) {
            com.huawei.hms.game.a aVar = this.t;
            if (!com.huawei.hms.game.f.a().a(this.n, aVar != null ? aVar.c() : "")) {
                return false;
            }
            int i = this.n.getResources().getConfiguration().orientation;
            if (i == 2) {
                int i2 = this.b.x;
                if (i2 == 0 || i2 == this.j) {
                    return true;
                }
            }
            if (i == 1) {
                i = this.b.y;
                if (i == 0 || i == this.i) {
                    return true;
                }
            }
        }
        return false;
    }

    /* JADX WARNING: Missing block: B:8:0x0031, code skipped:
            if (com.huawei.hms.game.h0.a(r2.b) != false) goto L_0x003c;
     */
    /* JADX WARNING: Missing block: B:10:0x003a, code skipped:
            if (com.huawei.hms.game.h0.a(r2.b) != false) goto L_0x003c;
     */
    /* JADX WARNING: Missing block: B:12:0x003e, code skipped:
            r0 = r2.v;
     */
    public final void m() {
        /*
        r2 = this;
        r0 = com.huawei.hms.game.f.a();
        r1 = r2.n;
        r0 = r0.e(r1);
        if (r0 == 0) goto L_0x0034;
    L_0x000c:
        r0 = r2.n;
        r0 = r0 instanceof android.app.Activity;
        if (r0 == 0) goto L_0x002b;
    L_0x0012:
        r0 = com.huawei.hms.game.f.a();
        r1 = r2.n;
        r1 = (android.app.Activity) r1;
        r0 = r0.b(r1);
        if (r0 != 0) goto L_0x0042;
    L_0x0020:
        r0 = com.huawei.hms.game.f.a();
        r1 = r2.n;
        r0 = r0.a(r1);
        goto L_0x0040;
    L_0x002b:
        r0 = r2.b;
        r0 = com.huawei.hms.game.h0.a(r0);
        if (r0 == 0) goto L_0x003e;
    L_0x0033:
        goto L_0x003c;
    L_0x0034:
        r0 = r2.b;
        r0 = com.huawei.hms.game.h0.a(r0);
        if (r0 == 0) goto L_0x003e;
    L_0x003c:
        r0 = 0;
        goto L_0x0040;
    L_0x003e:
        r0 = r2.v;
    L_0x0040:
        r2.v = r0;
    L_0x0042:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.f.b.a.a.a.b.g.m():void");
    }

    public final void n() {
        try {
            this.a.updateViewLayout(this, this.b);
        } catch (Exception e) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("updateViewLayoutPosition exception:");
            stringBuilder.append(e);
            h.b("FloatWindowSmallView", stringBuilder.toString());
        }
    }

    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        m();
        int i = this.w;
        int i2 = configuration.orientation;
        if (i != i2) {
            this.w = i2;
            setCenterXY(this.n);
            j();
            new Handler().postDelayed(new d(), 200);
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (getContext() == null) {
            return true;
        }
        int action = motionEvent.getAction();
        if (action != 0) {
            int i = 2;
            if (action == 1) {
                float rawX = motionEvent.getRawX();
                float rawY = motionEvent.getRawY();
                if (this.o) {
                    i.c().a();
                    if (this.s) {
                        setVisibility(4);
                        if (i.c().b(this.n)) {
                            Context context = this.n;
                            if (context != null) {
                                if (context.getPackageName().equals("com.huawei.gamebox")) {
                                    c.f.a.a.a.a.a(context, this.t, j.b().a(context)).show();
                                } else {
                                    String name = c.f.b.a.a.a.a.a.class.getName();
                                    i = BuoyBridgeActivity.b;
                                    Intent intent = new Intent(context, BuoyBridgeActivity.class);
                                    intent.putExtra(BridgeActivity.EXTRA_DELEGATE_CLASS_NAME, name);
                                    intent.putExtra(BridgeActivity.EXTRA_IS_FULLSCREEN, false);
                                    intent.addFlags(268435456);
                                    intent.putExtra("appInfo", this.t);
                                    context.startActivity(intent);
                                }
                            }
                        } else {
                            l.a().a(this.n, this.t, 2);
                            com.huawei.hms.game.d.a().b(getContext(), this.t);
                        }
                        if (j.b().a(getContext())) {
                            j.b().a(this.y);
                        }
                    } else {
                        b(rawX, rawY, motionEvent.getX(), motionEvent.getY());
                        if (com.huawei.hms.game.f.a().c(this.n) && l()) {
                            h();
                        }
                        a0 a = a0.a(getContext());
                        a.b(((float) (this.b.y + this.v)) / ((float) this.i));
                        a.a(((float) this.b.x) / ((float) this.j));
                        n();
                        i();
                    }
                } else {
                    c(m.l().b(), m.l().c());
                    i();
                    if (e0.b(this.n, this.t.c())) {
                        h.d("FloatWindowSmallView", "app is in background, start remove view");
                        k.b().a();
                    } else {
                        com.huawei.hms.game.d.a().a(this.n, this.t);
                        if (this.n.getResources().getConfiguration().orientation != 2) {
                            i = 1;
                        }
                        m.l().a(this.n, i);
                    }
                }
                this.o = false;
            } else if (action == 2) {
                this.c = motionEvent.getRawX();
                float rawY2 = motionEvent.getRawY() - ((float) this.v);
                this.d = rawY2;
                LayoutParams layoutParams = this.b;
                if (layoutParams != null) {
                    layoutParams.x = (int) (this.c - this.g);
                    layoutParams.y = (int) (rawY2 - this.h);
                    n();
                }
                if (!this.o) {
                    rawY2 = h0.a(this.n, 24);
                    Object obj = (Math.abs(this.e - this.c) > rawY2 || Math.abs(this.f - this.d) > rawY2) ? 1 : null;
                    if (obj != null) {
                        this.o = true;
                        f();
                        g(false);
                        i.c().b();
                    }
                }
                if (this.o) {
                    i c = i.c();
                    layoutParams = this.b;
                    if (c.a((float) layoutParams.x, (float) layoutParams.y)) {
                        i.c().a(true);
                        this.s = true;
                    } else {
                        i.c().a(false);
                        this.s = false;
                    }
                }
            }
        } else {
            this.g = motionEvent.getX();
            this.h = motionEvent.getY();
            this.e = motionEvent.getRawX();
            this.f = motionEvent.getRawY() - ((float) this.v);
            this.c = motionEvent.getRawX();
            this.d = motionEvent.getRawY() - ((float) this.v);
            this.o = false;
            f();
        }
        return true;
    }

    public void onWindowVisibilityChanged(int i) {
        super.onWindowVisibilityChanged(i);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("onWindowVisibilityChanged:");
        stringBuilder.append(i);
        String str = "FloatWindowSmallView";
        h.c(str, stringBuilder.toString());
        if (i == 8) {
            h.c(str, "onWindowVisibilityChanged, hideFloatWindow");
            i.c().a();
            if (l.a().a(this.n, this.t)) {
                j.b().a();
            }
            if (m.l().e()) {
                m.l().a(this.n);
            }
        }
    }
}
